package com.project.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.project.ds.ActivityLogMapper;
import com.project.ds.ActivityUserMapper;
import com.project.entity.activity.ActivityLog;
import com.project.entity.activity.ActivityUser;
import com.project.entity.enumcommon.ActivityLogStatusEnum;
import com.project.service.IActivityUserService;

/**
 * 活动实体类查询服务接口实现类
 * @author chenlz
 *
 */
@Service
@Transactional//后面配置具体的数据源的名称。例如：@Transactional(value="dataSourceTransactionManagerZnwlOrder")
public class ActivityUserServiceImpl implements IActivityUserService {
	
	private static final Logger logger = LoggerFactory.getLogger(ActivityUserServiceImpl.class);
	
	@Autowired
	private ActivityUserMapper activityUserMapper;
	
	@Autowired
	private ActivityLogMapper activityLogMapper;
	
	/**
	 * 保存
	 * @author chen.liangzhen
	 * @time 2017年3月30日 上午11:15:34
	 * @version	v0.1.0
	 * @param activityUser
	 * @return
	 */
	@Override
	public Long save(ActivityUser activityUser) {
		activityUserMapper.insert(activityUser);
		return activityUser.getId();
	}

	/**
	 * 对参加该活动的手机号进行查重，若是已有手机号参加该活动，提示该手机号已被使用
	 * @author chen.liangzhen
	 * @time 2017年3月30日 下午2:19:08
	 * @version v0.1.0
	 * @param activityId
	 * @param mobile
	 * @return
	 */
	@Override
	public int checkRepetitionApply(Long activityId, String mobile) {
		return activityUserMapper.checkRepetitionApply(activityId, mobile);
	}
	
	/**
	 * 每个账号只能报名一次
	 * @author chen.liangzhen
	 * @time 2017年3月30日 下午2:19:08
	 * @version v0.1.0
	 * @param activityId
	 * @param userCode
	 * @return
	 */
	@Override
	public int checkUserRepetitionApply(Long activityId, String userCode) {
		return activityUserMapper.checkUserRepetitionApply(activityId, userCode);
	}
	
	/**
	 * 保存活动用户信息
	 * @author chen.liangzhen
	 * @time 2017年3月30日 下午3:32:47
	 * @version v0.1.0
	 * @param activityUser
	 * @return
	 */
	@Override
    public int saveActivityUser(ActivityUser activityUser) {
		int activityUserId = activityUserMapper.saveActivityUser(activityUser);
		//写入报名的日志
		ActivityLog ActivityLog = new ActivityLog();
		ActivityLog.setActivityId(activityUser.getActivityId());
		ActivityLog.setActivityUserId(activityUser.getUserId());
		ActivityLog.setCreateTime(new Date());
		ActivityLog.setCreateUser(activityUser.getName());
		ActivityLog.setStatus(ActivityLogStatusEnum.DEFAULT.getValue());
		ActivityLog.setContent("活动报名");
		activityLogMapper.insert(ActivityLog);
    	return activityUserId;
    }
    
    /**
	 * 取消活动
	 * @author chen.liangzhen
	 * @time 2017年3月30日 下午4:23:33
	 * @version
	 * @param id
	 * @return
	 */
    @Override
	public boolean activityCancel(Long userId,Long activityId, String cancelReason) {
		boolean bln = true;
		try {
			ActivityUser activityUser = new ActivityUser();
			activityUser.setActivityId(activityId);
			activityUser.setUserId(userId);
			        
			ActivityUser au = activityUserMapper.find(activityUser);
			au.setCancelReason(cancelReason);
			au.setCancelTime(new Date());
			au.setAuditStatus(ActivityLogStatusEnum.CANCEL.getValue());
			activityUserMapper.update(au);
			
			//写入报名的日志
			ActivityLog activityLog = new ActivityLog();
			activityLog.setActivityId(activityId);
			activityLog.setActivityUserId(au.getId());
			activityLog.setCreateTime(new Date());
			activityLog.setCreateUser(au.getName());
			activityLog.setStatus(ActivityLogStatusEnum.CANCEL.getValue());
			activityLog.setContent("取消活动");
			activityLogMapper.insert(activityLog);
		} catch (Exception e) {
			bln = false;
			logger.info(e.getMessage(), e);
		}
		return bln;
	}
	
	/**
	 * 根据userId查询活动列表
	 * @author chen.liangzhen
	 * @time 2017年3月30日 下午4:41:52
	 * @version v0.1.0
	 * @param userId
	 * @return
	 */
	@Override
    public List<ActivityUser> queryByUserId(@Param("userId")Long userId) {
		return activityUserMapper.queryByUserId(userId);
    }
	
	/**
     * 根据实体类查询对应的数据集合
     * @author chen.liangzhen
     * @time 2017年4月5日 下午3:38:48
     * @version
     * @param activityUser
     * @return
     */
	@Override
    public List<ActivityUser> findList(ActivityUser activityUser) {
		return activityUserMapper.findList(activityUser);
	}
	
	/**
	 * 根据userId查询活动列表
	 * @author chen.liangzhen
	 * @time 2017年3月30日 下午4:41:52
	 * @version v0.1.0
	 * @param userId
	 * @return
	 */
	@Override
    public List<ActivityUser> queryByUserIdAndStatus(Long userId, String auditStatus, String status, int pageNo, int pageSize) {
		return activityUserMapper.queryByUserIdAndStatus(userId, auditStatus, status, pageNo, pageSize);
	}
}
